package com.example.no1admin.controller;

import com.example.no1admin.pojo.dto.packages.PackagesAddNewDTO;
import com.example.no1admin.pojo.dto.packages.PackagesEditDTO;
import com.example.no1admin.pojo.vo.packages.PackageMoreVO;
import com.example.no1admin.pojo.vo.packages.PackagesDetailListItemVO;
import com.example.no1admin.pojo.vo.packages.PackagesListItemVO;
import com.example.no1admin.service.IPackagesService;
import com.example.no1admin.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "套餐管理模块")
@RequestMapping("/packages")
@Slf4j
@RestController
public class PackageController {
    @Autowired
    IPackagesService packagesService;

    @ApiOperation("添加套餐")
    @PreAuthorize("hasAuthority('ROLE_admin')")
    @ApiOperationSupport(order = 100)
    @PostMapping("/add-new")
    public JsonResult addNew(@RequestBody PackagesAddNewDTO packagesAddNewDTO){
        log.debug("接受到的请求参数:{}",packagesAddNewDTO);
        packagesService.addNew(packagesAddNewDTO);
        return JsonResult.ok();
    }

    @ApiOperation("删除套餐")
    @PreAuthorize("hasAuthority('ROLE_admin')")
    @ApiOperationSupport(order = 200)
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult deleteById(@PathVariable Long id){
        log.debug("接收到删除套餐的请求");
        packagesService.deleteById(id);
        return JsonResult.ok();
    }

    @ApiOperation("修改套餐")
    @PreAuthorize("hasAuthority('ROLE_admin')")
    @ApiOperationSupport(order = 300)
    @PostMapping("/update")
    public JsonResult updateById(@RequestBody PackagesEditDTO packagesEditDTO){
        log.debug("接收到修改套餐的请求");
        packagesService.updateById(packagesEditDTO);
        return JsonResult.ok();
    }


    @ApiOperation("套餐列表")
    @ApiOperationSupport(order = 400)
    @GetMapping("")
    public JsonResult list(){
        log.debug("接收到查询套餐列表的请求");
        List<PackagesDetailListItemVO> packages = packagesService.list();
        return JsonResult.ok(packages);
    }

    @ApiOperation("关联查询列表")
    @ApiOperationSupport(order = 500)
    @GetMapping("/select")
    public JsonResult listItem(){
        log.debug("接收到关联查询套餐列表的请求");
        List<PackageMoreVO> packageMoreVOS = packagesService.listItem();
        return JsonResult.ok(packageMoreVOS);
    }


}
